package com.hdoit.kylin.web.user.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.group.service.IGroupService;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.user.TeacherSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

@Controller
@RequestMapping(value="/admin/user/teacher/")
public class TeacherController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/user/teacher/";
	}
	
	private ITeacherService teacherService;
	private IStudentService studentService;
	private IGroupService groupService;
	private IEmployeeService employeeService;

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public IGroupService getGroupService() {
		return groupService;
	}

	@Resource
	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String teacherDetailedViewName="teacherDetailed";
	private final String saveViewName="save";
	private final String deleteViewName="delete";
	private final String checkAccountViewName="checkAccount";
	
	@RequestMapping(value=indexViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,TeacherSearch teacherSearch,PagerModel pagerModel,boolean ajaxPost){
		String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		if(null==groupId){
			groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		}
		
		List<Teacher> teachers;
		if(null==groupId){
			pagerModel.setTotalAmount(teacherService.queryCount(teacherSearch));
			teachers=teacherService.queryList(teacherSearch,pagerModel);
		}
		else{
			pagerModel.setTotalAmount(teacherService.queryCountByGroupId(teacherSearch, groupId));
			teachers=teacherService.queryListByGroupId(teacherSearch,pagerModel,groupId);
		}
		
		model.addAttribute("teachers", teachers);
		model.addAttribute("teacherSearch", teacherSearch);
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		
		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value=teacherDetailedViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String teacherDetailed(Model model,String key){
		Teacher teacher;
		if(StringUtils.isEmpty(key)){
			teacher=new Teacher();
			teacher.setSex(SexStatus.male);
		}
		else{
			teacher=teacherService.queryModel(key);
		}
		
		model.addAttribute("teacher", teacher);
		
		return view(teacherDetailedViewName);
	}
	
	@RequestMapping(value=saveViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model,Teacher teacher){
		if(StringUtils.isEmpty(teacher.getEmployeeId())){
			teacher.setEmployeeId(CommonUtils.getKeyId());
			teacher.setEmployeeName(teacher.getTeacherName());
			teacher.setCreateDate(DateUtils.getCurrent());
			teacher.setCreateUserId(getLoginModel().getLoginId());
			teacher.setCreateUserName(getLoginModel().getLoginName());
			
			String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
			if(null==groupId){
				groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
			}
			if(!StringUtils.isEmpty(groupId)){
				teacher.setGroupId(groupId);
			}
			else{
				teacher.setGroupId("noGroup");
			}
			
			teacher.setModelStatus(ModelStatus.add);
		}
		else{
			teacher.setEmployeeName(teacher.getEmployeeAccount());
			teacher.setModifyDate(DateUtils.getCurrent());
			teacher.setModifyUserId(getLoginModel().getLoginId());
			teacher.setModifyUserName(getLoginModel().getLoginName());
			teacher.setModelStatus(ModelStatus.update);
		}
		teacher.setEmployeeCode(ChineseUtils.getPingYin(teacher.getTeacherName()));
		teacher.setMarkCode(ChineseUtils.getPinYinHeadChar(teacher.getTeacherName()));
		ActionResult actionResult;
		System.err.println(teacher.getRoleIds());
		if(teacherService.applyChange(teacher)){
			actionResult=new ActionResult();
		}
		else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("teacher", teacher);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(teacherDetailedViewName);
	}
	
	@RequestMapping(value=deleteViewName,method={RequestMethod.GET,RequestMethod.POST},produces = "text/json;charset=UTF-8")
	public @ResponseBody String delete(String key){
		if(teacherService.deleteModel(key)&&employeeService.deleteModel(key)){
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
	
	@RequestMapping(value = checkAccountViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkAccount(String employeeId,String employeeAccount){
		return String.valueOf(!employeeService.checkAccount(employeeId, employeeAccount));
	}
}
